/*
 * 项目名称:platform-plus
 * 类名称:SysJmCourseUserServiceImpl.java
 * 包名称:com.platform.modules.sys.service.impl
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2021-06-28 21:48:50        klwu     初版做成
 *
 * Copyright (c) 2019-2019 微同软件
 */
package com.platform.modules.sys.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.utils.Query;
import com.platform.modules.sys.dao.SysJmCourseDao;
import com.platform.modules.sys.dao.SysJmCourseUserDao;
import com.platform.modules.sys.dao.SysUserDao;
import com.platform.modules.sys.entity.SysJmCourseEntity;
import com.platform.modules.sys.entity.SysJmCourseUserEntity;
import com.platform.modules.sys.entity.SysUserEntity;
import com.platform.modules.sys.service.SysJmCourseService;
import com.platform.modules.sys.service.SysJmCourseUserService;
import com.platform.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 课程任务学生表Service实现类
 *
 * @author klwu
 * @date 2021-06-28 21:48:50
 */
@Service("sysJmCourseUserService")
public class SysJmCourseUserServiceImpl extends ServiceImpl<SysJmCourseUserDao, SysJmCourseUserEntity> implements SysJmCourseUserService {

    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SysJmCourseDao sysJmCourseDao;

    @Override
    public List<SysJmCourseUserEntity> queryAll(Map<String, Object> params) {
        return baseMapper.queryAll(params);
    }

    @Override
    public Page queryPage(Map<String, Object> params) {
        //排序

        params.put("asc", false);
        Page<SysJmCourseUserEntity> page = new Query<SysJmCourseUserEntity>(params).getPage();
        if(params.get("module").equals("2")){
            List<SysJmCourseUserEntity> list = baseMapper.selectStuCourseModuleRecords(page, params);
            for(SysJmCourseUserEntity jmCourseUserEntity : list){
                String courseId = jmCourseUserEntity.getCourseId();
                String userId = params.get("userId").toString();
                SysUserEntity user = sysUserDao.selectById(userId);
                SysJmCourseEntity course = sysJmCourseDao.selectById(courseId);
                jmCourseUserEntity.setUserName(user.getRealName());
                jmCourseUserEntity.setCourseDate(course.getCourseDate());
                jmCourseUserEntity.setCourseAddress(course.getCourseAddress());
                jmCourseUserEntity.setCourseName(course.getCourseName());
                jmCourseUserEntity.setCourseCode(course.getCourseCode());
                jmCourseUserEntity.setTeacherName(course.getCourseTeacher());
            }
            return page.setRecords(list);
        }
        params.put("sidx", "T.id");
        return page.setRecords(baseMapper.selectSysJmCourseUserPage(page, params));
    }

    /**
     * 必选课学生列表
     * @param params
     * @return
     */
    @Override
    public Page queryPage2(Map<String, Object> params) {
        //排序
        //params.put("sidx", "T.id");
        //params.put("asc", false);
        Page<SysUserEntity> page = new Query<SysUserEntity>(params).getPage();
        params.put("tempFlag",1);
        List<SysUserEntity> sysUserEntities = sysUserDao.selectListPage(page, params);
        String courseId = params.get("courseId").toString();
        SysJmCourseEntity sysJmCourseEntity = sysJmCourseDao.selectById(courseId);
        for(SysUserEntity user : sysUserEntities){
            user.setCourseName(sysJmCourseEntity.getCourseName());
            user.setCourseCode(sysJmCourseEntity.getCourseCode());
            user.setCourseAddress(sysJmCourseEntity.getCourseAddress());
            user.setCourseTime(sysJmCourseEntity.getCourseDate());
            user.setTeacherName(sysJmCourseEntity.getCourseTeacher());
        }
        return page.setRecords(sysUserEntities);
    }

    @Override
    public boolean add(SysJmCourseUserEntity sysJmCourseUser) {
        return this.save(sysJmCourseUser);
    }

    @Override
    public boolean update(SysJmCourseUserEntity sysJmCourseUser) {
        return this.updateById(sysJmCourseUser);
    }

    @Override
    public boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(String[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }
}
